package demo;

public class thr {
    public static void main(String args[]) {
        Wugui wu = new Wugui();
        Tuzi tu = new Tuzi();
        tu.setName("兔子");
        wu.setName("乌龟");

        tu.start();
        wu.start();
        System.out.println("兔子、乌龟跑起来了");
    }
}

class Wugui extends Thread {
    int meters = 0;

    public void run() {
        while (true) {
            String name = Thread.currentThread().getName();
            int r1 = (int) (Math.random() * (100 - 0) + 0);
            if (r1 >= 0 && r1 <= 50) {
                meters++;
                System.out.println(name + "跑了" + meters + "米");
            } else {
                if (r1 >= 60 && r1 <= 80) {
                    System.out.println(name + "休息了1s");
                    try {
                        Thread.sleep(1000);  //间隔时间
                    } catch (InterruptedException e) {
                    }

                } else {
                    System.out.println(name + "休息了2s");
                    try {
                        Thread.sleep(2000);  //间隔时间
                    } catch (InterruptedException e) {
                    }

                }
            }
            if (meters == 20) {
                System.out.println("乌龟到达终点！！！");
                System.out.println("兔子输了！！！");
                return;
            }
        }
    }
}

class Tuzi extends Thread {
    int meters = 0;
    boolean b = true;

    public void run() {

        while (true) {

            String name = Thread.currentThread().getName();

            int r1 = (int) (Math.random() * (100 - 0) + 0);
            if (r1 >= 0 && r1 <= 50 && b == false) {
                meters += (int) (Math.random() * (3 - 1) + 1);
                System.out.println(name + "跑了" + meters + "米");
            } else {
                if (b == true) {
                    b = false;
                    System.out.println(name + "休息了10s");
                    try {
                        Thread.sleep(10000);  //间隔时间
                    } catch (InterruptedException e) {
                    }
                } else {
                    System.out.println(name + "休息了1s");
                    try {
                        Thread.sleep(1000);  //间隔时间
                    } catch (InterruptedException e) {
                    }
                }
            }
            if (meters == 20) {
                System.out.println("兔子到达终点！！！");
                System.out.println("乌龟输了！！！");
                return;
            }
        }

    }
}